//
//  UsedAsView.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/18.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "UsedAsView.h"
#import "UIButton+BaseAttributes.h"
#import "UILabel+BasicAttributes.h"
#import "UseModel.h"

@interface UsedAsView()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (nonatomic,strong)UIView *dimView;
@property (nonatomic,strong)UIView *baseView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UIPickerView *pickView;
@property (nonatomic,strong)NSArray *usedArr;
@property (nonatomic,strong)UseModel *model;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,assign)NSInteger index1;
@property (nonatomic,assign)BOOL firstLoad;
//@property (nonatomic,strong)UIView *lineView;
@end

@implementation UsedAsView
//- (UIView *)lineView
//{
//    if (!_lineView) {
//        _lineView = [[UIView alloc] initWithFrame:CGRectMake(kScreenWidth*0.9, kScreenHeight, kScreenWidth*0.1, kScreenHeight)];
//        _lineView.backgroundColor = [UIColor whiteColor];
//        [self addSubview:_lineView];
//    }
//    return _lineView;
//}
- (NSArray *)usedArr
{
    if (!_usedArr) {
        _usedArr = [NSArray new];
    }
    return _usedArr;
}
- (UIPickerView *)pickView
{
    if (!_pickView) {
        _pickView = [[UIPickerView alloc] init];
        _pickView.dataSource = self;
        _pickView.delegate = self;
    }
    return _pickView;
}
- (UIView *)dimView
{
    if(!_dimView) {
        _dimView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        _dimView.alpha = 0.0f;
        _dimView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_dimView];
    }
    return _dimView;
}
- (UIView *)baseView
{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4)];
        _baseView.alpha = 1.0f;
        _baseView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_baseView];
    }
    return _baseView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    _firstLoad = YES;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
        self.dimView.alpha = 0.20f;
    }];
    UIButton *cancelBtn = [UIButton bttonWithTitle:@"取消" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [cancelBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    
    
    UIButton *confirmBtn = [UIButton bttonWithTitle:@"完成" andNormalTitleColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] selectColor:[UIColor colorWithRed:0.33f green:0.59f blue:0.87f alpha:1.00f] titleFont:BigFontSize];
    [confirmBtn addTarget:self action:@selector(buttonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [_baseView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(_baseView);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(100);
    }];
    
    _titleLabel = [UILabel LabelWithFont:BigFontSize andColor:[UIColor colorWithRed:0.38f green:0.38f blue:0.38f alpha:1.00f] andTextAlignment:center];
    _titleLabel.text = self.title;
    [_baseView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(cancelBtn.mas_right);
        make.right.mas_equalTo(confirmBtn.mas_left);
        make.centerY.mas_equalTo(confirmBtn);
    }];
    
    [_baseView addSubview:self.pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(_baseView);
        make.top.mas_equalTo(cancelBtn.mas_bottom);
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
     _titleLabel.text = _title;
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self hidde];
}
- (void)show
{
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight - kScreenHeight*0.4, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kScreenHeight*0.4);
        self.dimView.alpha = 0.20f;
    }];
}
- (void)hidde
{
    if (_viewHadRemoveFromeSup) {
        _viewHadRemoveFromeSup();
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        self.baseView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kScreenHeight*0.4);
        self.dimView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.dimView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        self.hidden = YES;
    }];
}
- (void)selfRemoveToSub
{
   
}
- (void)buttonDidClick:(UIButton *)sender
{
    [self hidde];
    if ([sender.titleLabel.text isEqualToString:@"取消"]) {
        
    }else{
        _index = _index1;
        UseModel *model = [UseModel new];
        if (self.usedArr.count&&self.usedArr.count>_index) {
            NSDictionary *dic = self.usedArr[_index];
            model.text = dic[@"text"];
            model.userId = dic[@"id"];
        }else{
            model.text = @"全部";
            model.userId = @"";
        }
        if (_resultModel) {
            _resultModel(model);
        }
    }
}
#pragma mark - UIPickView 代理 数据源
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.usedArr.count+1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
        if (row<self.usedArr.count) {
            NSDictionary *dic = self.usedArr[row];
            return dic[@"text"];
        }else{
            return @"全部";
        }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _index1 = row;
    if (row==self.usedArr.count||self.usedArr.count==0) {
        self.model.text = @"全部";
        self.model.userId = @"";
    }else{
        NSDictionary *dic = self.usedArr[row];
        self.model.text = dic[@"text"];
        self.model.userId = dic[@"id"];;
    }


}
- (void)loadUsedBottomViewWithUsedArr:(NSArray *)arr
{
    
    if (_firstLoad) {
        self.usedArr = [NSArray new];
        self.usedArr = arr;
        _index = self.usedArr.count;
        _firstLoad = NO;
    }
    UseModel *model = [UseModel new];
    if (self.usedArr.count == 0|| _index == self.usedArr.count) {
        model.text = @"全部";
        model.userId = @"";
    }else{
        NSDictionary *dic = self.usedArr[_index];
        model.text = dic[@"text"];
        model.userId = dic[@"id"];;
    }
    if (_resultModel) {
        _resultModel(model);
    }
    
    [self.pickView reloadAllComponents];
    [self.pickView selectRow:_index inComponent:0 animated:YES];
}


@end
